<?php
require_once("../DAO/UserDAO.php");
include_once("../Entities/History.php");
include_once("../DAO/HistoryDAO.php");
include_once("../Utility/WebUtilities.php");

include_once("../Utility/WebSession.php");

if(WebSession::CurrentUserNoReturn() == null)
{
	WebUtilities::Redirect("index.php");
}

$historyDAO = new HistoryDAO();

$dateFrom = WebUtilities::GetQueryString("dateFrom", "1/1/1999");
$dateTo = WebUtilities::GetQueryString("dateTo", date("d/m/Y"));
$pageSize = WebUtilities::GetPageSize();
$page = WebUtilities::GetQueryString("page", 1);
$totalPage = 0;
$historyList = $historyDAO->GetHistoris($page, $pageSize, $totalPage, WebSession::CurrentUserNoReturn()->UserId, $dateFrom, $dateTo);

$historyStr = "";
if(count($historyList) == 0)
{
	$historyStr = "<tr><td colspan='6'>No items</td></tr>";
}
else
{
	for($i = 0; $i < count($historyList); $i++)
	{
		$item = $historyList[$i];
		$historyStr = $historyStr . "<tr><td>" . $item->Id . "</td>" 
								  . "<td>" . $item->CreationOn . "</td>"
								  . "<td>" . $item->TotalCorrect . "</td>"
								  . "<td>" . $item->TotalQuestions . "</td>"
								  . "<td>" . $item->Marks . "</td>"	
								  . "<td>" . $item->TotalMarks . "</td></tr>";					
	}
}

$submitUrl = "History.php?page=" . $page;
$pageUrlFirst = "<a href='History.php?page=1'>First</a>";
$pageUrlLast = "<a href='History.php?page=" . $totalPage ."'>Last</a>";
$pageUrlNext = "<a href='History.php?page=" . ($page + 1) ."'>Next ></a>";
$pageUrlPrev = "<a href='History.php?page=" . ($page - 1) ."'>< Prev</a>";
if($page <= 1)
{
	$pageUrlFirst = "First";
	$pageUrlPrev = "< Prev";
}
if($page >= $totalPage)
{
	$pageUrlLast = "Last";
	$pageUrlNext = "Next >";
}

$mainContent = "<form enctype='multipart/form-data' action='" . $submitUrl ."' method='POST'>
					<div class='oneLine'>
						<label>From  </label><input name='dateFrom' id='dateFrom' class='date-pick' readonly='true' />
						<label>To  </label><input name='dateTo' id='dateTo' class='date-pick' readonly='true'/>
						<input value='Search' type='button' onclick=''/>
					</div>
					<br/>
					<table class='manager' id='tableAnswers'>
						<thead>
							<tr>
								<th>History Id</th>
								<th>Creation On</th>
								<th>Correct Questions</th>
								<th>Total Questions</th>
								<th>Marks</th>
								<th>Total Marks</th>
							</tr>
						</thead>
						<tbody class='tableBody'>"
							. $historyStr . 
						"</tbody>
					</table>" .
				"<div class='paging'>
					Page " . ($page >= $totalPage ? $totalPage : $page) . " of " . $totalPage . " " . 
					$pageUrlFirst . " | " . $pageUrlPrev . " | " . $pageUrlNext . " | " . $pageUrlLast . 
				"</form>";
$scriptContent = "$('.date-pick').datePicker({startDate:'01/01/1996'});";
include("MasterPage.php");
?>